﻿from django.http import HttpResponse, HttpResponseRedirect
from django.core.paginator import ObjectPaginator, InvalidPage
from app.engine import models
from google.appengine.api import users
from google.appengine.ext import db
from app.functions.funcs import render,split_tags,formate_time,gen_duration
from google.appengine.api import memcache
from django.utils import simplejson

def index(request):
    user = users.get_current_user()
    if user:
        items = models.Card.gql("ORDER BY category desc,number asc")
    else:
        return HttpResponseRedirect(users.create_login_url("/"))
    payload = dict(items = items)
    return render('index.html', payload)

def console_compute_card(request):
    user = users.get_current_user()
    if user:
        owner = models.UserInfo.all().filter('email =', user.email())
        if owner.count()>0:
            owner = owner[0]
        else:
            return HttpResponseRedirect("/console/userinfo/")
    payload = dict(owner = owner)
    return render('console_compute_card.html',payload)

def console_init(request,items_json=None):
    user = users.get_current_user()
    items = models.Card.gql("ORDER BY category desc,number asc")
    owner = models.UserInfo.all().filter('email =', user.email())
    if owner.count():
        owner = owner[0]
        useritems = models.UserItems.all().filter('owner =', user)
        if useritems.count() == 0:
            if request.method == 'POST':
                user_items = models.UserItems(data=items_json,owner=user)
                user_items.put()
        elif useritems.count() == 1:
            if request.method == 'POST':
                useritems=useritems[0]
                useritems.data = items_json
                useritems.put()
        else:
            pass
    else:
        return HttpResponseRedirect("/console/userinfo/")
    payload = dict(items=items,useritems=useritems)
    return render('console_init_cards.html', payload)

def console_userinfo(request):
    user = users.get_current_user()
    if user:
        owner = models.UserInfo.all().filter('email =', user.email())
        if owner.count()>0:
            owner = owner[0]
            if request.method == 'POST':
                owner = models.UserInfo(charactor = request.POST.get('charactor').decode('utf-8'),
                                        region = request.POST.get('region'),
                                        realm = request.POST.get('realm').decode('utf-8'),
                                        email = user.email())
                owner.put()
                return HttpResponseRedirect("/console/card/")
        else:
            if request.method == 'POST':
                owner = models.UserInfo(charactor = request.POST.get('charactor').decode('utf-8'),
                                        region = request.POST.get('region'),
                                        realm = request.POST.get('realm').decode('utf-8'),
                                        email = user.email())
                owner.put()
                return HttpResponseRedirect("/console/card/")
    else:
        return HttpResponseRedirect(users.create_login_url("/"))
    payload = dict(owner=owner)
    return render('console_userinfo.html',payload)

def jsonUserInfo(request,user_id):
    return HttpResponse(simplejson.dumps(my_spreadsheets.feed_dict),mimetype='application/json')